package com.csi.controller;

import com.csi.domain.Hotel;
import com.csi.domain.TravelAttraction;
import com.csi.domain.TravelPackage;
import com.csi.domain.Trip;
import com.csi.service.TravelService;
import com.csi.utils.JsonResult;
import com.csi.utils.JwtUtil;
import com.csi.utils.ReturnResults;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/travel")
@Tag(name = "Travel",description = "旅游相关接口API")
public class TravelController {

    @Autowired
    private TravelService travelService;

    /**
     * 查询不同类型的旅游套餐
     * @return
     */
    @GetMapping("/type")
    @Operation(summary = "查询不同类型的旅游套餐",description = "查询不同类型的旅游套餐")
    public Object type() {
        Map<String, List<Map<String,Object>>> listMap = travelService.findByType();
        if (listMap != null && listMap.size() > 0) {
            return new JsonResult<>(listMap);
        }
        return new JsonResult(ReturnResults.ACCESS_FAILED,"失败了");
    }

    /**
     * 查询旅游套餐详情
     * @param id
     * @return
     */
    @GetMapping("/packageInfo")
    @Operation(summary = "旅游套餐详情",description = "旅游套餐详情")
    public Object packageInfo(@RequestParam("packageId") Long id) {
        TravelPackage travelPackage = travelService.packageInfo(id);
        if (travelPackage != null) {
            return new JsonResult<>(travelPackage);
        }
        return new JsonResult(ReturnResults.ACCESS_FAILED,"失败了");
    }

    @GetMapping("/trip")
    @Operation(summary = "旅游套餐行程",description = "旅游套餐行程")
    public Object trip(@RequestParam("packageId") Long id) {
        Trip trip = travelService.trip(id);
        if (trip != null) {
            return new JsonResult<>(trip);
        }
        return new JsonResult(ReturnResults.ACCESS_FAILED,"失败了");
    }

    @GetMapping("/findAll")
    @Operation(summary = "查询全部旅游套餐",description = "查询全部旅游套餐")
    public Object findAll() {
        List<Map<String,Object>> list = travelService.findAll();
        if (list != null && list.size() > 0) {
            return new JsonResult<>(list);
        }
        return new JsonResult(ReturnResults.ACCESS_FAILED,"失败了");
    }

    @GetMapping("/travelHotel")
    @Operation(summary = "查询套餐下的酒店",description = "查询套餐下的酒店")
    public Object travelHotel(@RequestParam("travelId") Long id) {
        Hotel hotel = travelService.travelHotel(id);
        if (hotel != null) {
            return new JsonResult<>(hotel);
        }
        return new JsonResult(ReturnResults.ACCESS_FAILED,"失败了");
    }

    @GetMapping("/travelAttraction")
    @Operation(summary = "查询套餐下的景点信息",description = "查询套餐下的景点信息")
    public Object travelAttraction(@RequestParam("travelId") Long id) {
        List<TravelAttraction> list = travelService.travelAttraction(id);
        if (list != null && list.size() > 0) {
            return new JsonResult<>(list);
        }
        return new JsonResult(ReturnResults.ACCESS_FAILED,"失败了");
    }

    @GetMapping("/findName")
    public Object findName(HttpServletRequest request){
        String token = request.getHeader("token");
        long travelId = JwtUtil.getTokenBody(token).get("travelId", Long.class);
        String travelName = travelService.findName(travelId);
        if (travelName != null){
            return new JsonResult<>(travelName);
        }
        return new JsonResult(ReturnResults.ACCESS_FAILED,"查询失败");
    }

    @GetMapping("/findPackage")
    public Object findPackage(HttpServletRequest request){
        String token = request.getHeader("token");
        long travelId = JwtUtil.getTokenBody(token).get("travelId", Long.class);
        List<TravelPackage> travelPackage =  travelService.findPackage(travelId);
        if (travelPackage != null){
            return new JsonResult<>(travelPackage);
        }
        return new JsonResult<>(ReturnResults.ACCESS_FAILED,"查询失败");
    }

}
